﻿using GN.Pay.TradeChannel.Gyb.Response;
using GN.Pay.Utils;
using GN.Pay.Utils.Net.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.Gyb.Request
{
    /// <summary>
    /// 查询请求
    /// </summary>
    [Serializable]
    class QueryRequest : ARequest
    {
        private ITradeChannel channel;
        private GybConfigure config;
        private readonly DateTime queryDate;

        /// <summary>
        /// 实例化 QueryRequest 类新实例
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="queryDate"></param>
        /// <param name="config"></param>
        /// <param name="termSsn"></param>
        public QueryRequest(ITradeChannel channel, DateTime queryDate,
           GybConfigure config, string termSsn) : base(config)
        {
            this.channel = channel;
            this.config = config;
            if (string.IsNullOrWhiteSpace(termSsn))
            {
                channel.ThrowChannelValidationException("请求的 termSsn(订单号) 为 null");
            }
            this.queryDate = queryDate;
            OTranAbbr = TradeCommand.IPER;
            TermSsn = termSsn;
        }

        /// <summary>
        /// 原交易缩写
        /// </summary>
        public string OTranAbbr { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public override string TranAbbr
        {
            get
            {
                return TradeCommand.IPER;
            }
        }

        /// <summary>
        /// 订单号
        /// </summary>
        public string TermSsn { get; set; }

        /// <summary>
        /// 生成响应
        /// </summary>
        /// <returns></returns>
        public QueryResponse Build()
        {
            var plain = string.Format("{0}={1}|{2}={3}|{4}={5}", "MercCode", MercCode, "OTranAbbr", OTranAbbr, "TermSsn", TermSsn);
            SignDictionary dic = new SignDictionary();
            dic["transName"] = TradeCommand.IQSR;
            dic["Plain"] = plain;
            dic["Signature"] = SecurityUtils.Sign(config.ConfigureInfo, plain);

            //多笔查询需要转入网关订单号
            //var plain = string.Format("{0}={1}|{2}={3}|{4}={5}|{6}={7}", "MercCode", MercCode, "OTranAbbr", OTranAbbr, "BTermSsn", "60000021263422", "ETermSsn", "60000021263422");
            //SignDictionary dic = new SignDictionary();
            //dic["transName"] = "BQSR";
            //dic["Plain"] = plain;
            //dic["Signature"] = SecurityUtils.Sign(config.ConfigureInfo, plain);

            string requestContent = BuildXML(dic, config);

            HttpClient client = new HttpClient();
            client.Encoding = config.Encoding;
            client.ContentType = "text/xml";
            var response = client.PostString(new Uri(config.GatewayUrl), requestContent);

            //var response = HttpUtils.PostString(config.GatewayUrl, dic, config.Encoding);
            return new QueryResponse(response, this.channel, this.config);
        }
    }
}
